package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class QuestionActivity extends Activity {
	ArrayList<NameEntity> questions;
	ArrayList<Item> questionItems;
	MyCustomAdapter dataAdapter;
	int parent_id ;
	TextView uniId,fullName,courseLabel,questionLabel;
	EditText answer;
	Question current;
	CheckBox ispublic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question);
		Bundle extras = getIntent().getExtras();
		parent_id = extras.getInt("parent_id");
		uniId = (TextView) findViewById(R.id.uniId);
		courseLabel = (TextView) findViewById(R.id.courseLabel);
		fullName = (TextView) findViewById(R.id.fullName);
		questionLabel = (TextView) findViewById(R.id.questionLabel);
		answer = (EditText) findViewById(R.id.answer);
		ispublic = (CheckBox) findViewById(R.id.ispublic);
		if(!sip.isInstructor())
		{
			ispublic.setVisibility(View.GONE);
		}
		//setChildQuestions("",""); 
		getQuestion();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.question, menu);
		return true;
	}
	public void getQuestion()
	{
		String serverURL = new myURL("read_questions.php?id="+parent_id,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setQuestion", true);
		p.get();
	}
	public void setQuestion(String s, String error)
	{
		current = new APIManager().getQuestions(s).get(0);
		uniId.setText(current.getId()+"");
		fullName.setText(current.getStudent()+"");
		uniId.setText(current.getId()+"");
		courseLabel.setText(current.getCourse()+"");
		questionLabel.setText(current.getTitle()+"");
		answer.setText(current.getAnswer()+"");
		ispublic.setChecked(current.isPublic());
		Log.d("ray","Course info "+s);
		getChildQuestions();
	}

	public void getChildQuestions()
	{
		String serverURL = new myURL("read_questions.php?parent_id="+parent_id,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setChildQuestions", true);
		p.get();
	}
	
	public void setChildQuestions(String s, String error) {
		Log.d("ray", "reply: " + s);
		NameEntity ne = null;
		questions = new ArrayList<NameEntity>();
		ArrayList<Question> myCs = new APIManager().getQuestions(s);
		for(int i =0;i<myCs.size();i++)
		{
			Log.d("ray"," Course id" +myCs.get(i).getId() + " ,name: "+myCs.get(i).toString());
			ne = new NameEntity(myCs.get(i).getId(), myCs.get(i).toString(), "question");
			ne.setDescription(myCs.get(i).getAnswer());
			questions.add(ne);
		}
		questionItems = new ArrayList<Item>();
		ListView listView = (ListView) findViewById(R.id.list);

		listView.setTextFilterEnabled(true);
		if (questions.size() == 0) {
			questionItems.add(new Item(0, "", getString(R.string.empty_list)));
		} else {
			for (int i = 0; i < questions.size(); i++) {
				questionItems.add(new Item(questions.get(i).getId(), "",
						questions.get(i).toString()));
			}
			registerForContextMenu(listView);
			dataAdapter = new MyCustomAdapter(this, R.layout.child_question_list,
					questionItems);
			SharedMenu.adapter = dataAdapter;
			listView.setAdapter(dataAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (dataAdapter.tmpList.size() > 0) {
						Intent i;
						i = new Intent(getBaseContext(),QuestionActivity.class);
						i.putExtra("parent_id", dataAdapter.tmpList.get(position).getId());
						startActivity(i);

					}
				}
			});
			
		}
	}
	public void answerQuestion(View v)
	{
		String serverURL = new myURL("answer_question.php?id="+parent_id+"&answer="+answer.getText().toString()+"&public="+ispublic.isChecked(),true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "done", true);
		p.get();
	}
	public void done(String s,String error)
	{
		Toast.makeText(getApplicationContext(), "Answered successfully", Toast.LENGTH_SHORT)
	      .show();
	}
}
